import { getList as getUserList } from '@/services/system/user';
import { getSelectList, SelectType } from '@/util/util';
import { useModel } from '@@/exports';
import { useEffect, useState } from 'react';

const useUserList = () => {
  const { initialState, setInitialState } = useModel('@@initialState');
  const [userList, setUserList] = useState<SelectType[]>([]);
  const saveUserList = async () => {
    const data = await getSelectList<Omit<USER_TYPE.Info, 'roleId'>>({
      label: 'title',
      value: 'id',
      server: getUserList,
    });
    if (data) {
      setUserList(data);
      setInitialState((s) => {
        return {
          ...s,
          userList: data,
        };
      });
    }
  };
  useEffect(() => {
    if (initialState?.userList) {
      setUserList(initialState?.userList);
    } else {
      saveUserList();
    }
  }, []);

  return {
    userList,
    saveUserList,
  };
};
export default useUserList;
